﻿@page "/counter"
@using Microsoft.AspNetCore.Components.Server.Circuits
@rendermode InteractiveServer
@inject CircuitHandler CircuitTracker
@inject IJSRuntime _jSRuntime

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @circuitTracker.ConnectionCount</p>
<p role="status">Visitor count: @circuitTracker.VisitorCount</p>
<p role="status">CircuitId: @circuitTracker.CircuitId</p>
<p role="status">VisitorId: @circuitTracker.VisitorId</p>

@code{
    private CircuitTracker circuitTracker;

    protected override void OnInitialized()
    {
        circuitTracker = (CircuitTracker)CircuitTracker;
        base.OnInitialized();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            var visitorId = await _jSRuntime.InvokeAsync<string>("getVisitorId");
            (CircuitTracker as CircuitTracker)!.VisitorRegister(visitorId);
            StateHasChanged();
        }
        await base.OnAfterRenderAsync(firstRender);
    }
}